import java.sql.SQLOutput;

//节点类
class Node{
    //存储元素
    int data;
    //存储下一个元素的地址
    Node next;

    public Node(int data) {
        this.data = data;
    }

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }
}

//链表类
public class SingleLinkedList {
    //链表长度
    private int size;
    //头节点
    private Node head;

    public int getSize() {
        return size;
    }

    public void addFirst(int data){
        if(size == 0){
            Node node = new Node(data);//根据传入的参数判断调用哪个构造方法
            head = node;
            size++;
        }else{
            Node node = new Node(data);
            node.next = head;
            head = node;
            size++;
        }
    }
    public void addLast(int data){
        if(size == 0){
            Node node = new Node(data);//根据传入的参数判断调用哪个构造方法
            head = node;
            size++;
        }else{
            Node node = new Node(data);
            Node p = head;
            if(p != null){
                while (p.next != null){
                    p = p.next;
                }
                p.next = node;
            }
            size++;
        }
    }
    public void addIndex(int index,int data){
        //若链表为空
        Node node = new Node(data);
        if(index == 0 || head.next == null){
            addFirst(data);
        }else {
            Node prev = head;
            for (int i = 0; i < index - 1; i++) {
                prev = prev.next;
            }
            node.next = prev.next;
            prev.next = node;
            size++;
        }
    }
    //判断是否为有效数据
    private boolean rangeCheck(int index){
        if(index < 0 || index >= size){
            return false;
        }
        return true;
    }
    /**
     * 查询index位置的节点数据
     * @param index
     * @return
     */
    public int get(int index){
        if(rangeCheck(index)){
            Node node = head;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            //此时node指向待查找元素
            int data = node.data;
            return data;
        }else {
            System.err.println("get index illegal!");
            return -1;
        }
    }
    /**
     * 修改单链表中index位置的元素
     * 返回修改前的元素值
     * @param index
     * @param data
     * @return
     */
    public int set(int index,int data){
        if(rangeCheck(index)){
            Node node = head;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            int oldData = node.data;
            node.data = data;
            return oldData;
        }else {
            System.err.println("get index illegal!");
            return -1;
        }
    }
    /**
     * 判断单链表中是否包含元素data
     * @param data
     * @return
     */
    public boolean contains(int data){
        Node node = head;
        while(node != null){
            if(node.data == data){
                System.out.println("找到元素！");
                return true;
            }
            node = node.next;
        }
        System.out.println("没有找到该元素...");
        return false;
    }
    //头删
    public void removeFirst(){
        Node node = head;
        head = head.next;
        node.next = null;//脱钩操作
        size--;
    }
    public void remove(int index){
        if (rangeCheck(index)){
            if(index == 0){
                removeFirst();
            }else{
                Node prev = head;
                for (int i = 0; i < index - 1; i++) {
                    prev = prev.next;
                }
                Node node = prev.next;
                prev.next = node.next;
                node.next = null;
                size--;
            }
        }else {
            System.err.println("remove index illegal");
        }
    }

    /**
     * 删除链表中指定元素的第一个节点
     * @param data
     */
    public void removeValueOnce(int data){
        if(head.data == data){
            removeFirst();
        }else {
            Node prev = head;
            while (prev.next != null){
                if(prev.next.data == data){
                    Node node = prev.next;
                    prev.next = node.next;
                    node.next = null;
                    size--;
                }
            }
        }
    }
    public void removeValue(int data){
        // 判断头节点的情况
        // 头节点以及之后出现了多个连续待删除的节点
        while (head != null && head.data == data){
            Node node = head;
            head = node.next;
            node.next = null;
            size--;
        }
        // 此时head一定不是待删除的节点
        // head.data != data
        if (head == null) {
            // 此时链表全部是待删除的节点，全部删除完毕
            return;
        }else {
            // 此时头节点已经处理完毕，并且链表也不为空，向下一个节点开始判断
            Node prev = head;
            while (prev.next != null) {
                if (prev.next.data == data) {
                    // node就是待删除的节点
                    Node node = prev.next;
                    prev.next = node.next;
                    node.next = null;
                    size --;
                }else {
                    // 此时prev的下一个节点不是要删除的节点，prev继续向后走一个单位
                    prev = prev.next;
                }
            }
        }
    }
    //单链表的插入和删除都需要考虑头部元素是否存在的情况，有啥办法优化？


    @Override
    public String toString(){
        String ret = "";
        Node node = head;
        while(node != null){
            ret += node.data + "->";
            node = node.next;
        }
        ret +="NULL";
        return ret;
    }

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(2);
        System.out.println(singleLinkedList);
        singleLinkedList.removeValue(2);
        // 3
        System.out.println(singleLinkedList);
    }
}
